local ClientOHOH = _G.ClientOHOH;
local M = ClientOHOH.class.new(script, ClientOHOH.files.BaseComponent());
local CommonOHOH = _G.CommonOHOH;

function M.new(self,...)
	return M(self,...);
end;

function M.ctor(self, parent)
	M.super.ctor(self, parent);
 
    self.ConList={}
    
   if not self.parent:IsInBattle() and self.parent.character  then --不在战斗中
        self:Go(self.parent.character)
   end

   local con= self.parent.CharacterEnterEvent:Connect(function(Character)
        self:AddProcess(function()
            self:Wait(1)
            if not self.parent:IsInBattle()  then --不在战斗中
                self:Go(Character)
            end
        end)
   end)

   table.insert(self.ConList,con)

   local con2= self.parent.BattleSateEvent:Connect(function(isBattle)
        if not isBattle then
            self:AddProcess(function()
                self:Wait(1)
                self:Go(self.parent.character)
            end)
        else
            if self.RunCtrl then
                self.RunCtrl:StopRun()
            end

        end
    end)

   table.insert(self.ConList,con2)

   
end;

function M.dtor(self)
    for _,v in pairs(self.ConList) do
        v:Disconnect()
    end
    self.ConList={}
    if self.RunCtrl then
        self.RunCtrl:dtor()
        self.RunCtrl=nil
    end
 
	M.super.dtor(self);
end;



function M.Go(self,character)
    local ready=_G.client:GetComponent(_G.CommonOHOH.load.ReayBattleCtrl()):FindNearBattleReady(character); 
    self.parent.character:SetCFrame(ready:GetExitCFrame())
    self.RunCtrl= character:AddComponent(CommonOHOH.load.CharacterRun()); 
    self.RunCtrl:Run({ready:GetEnterPos()})
    local con=  self.RunCtrl.GoLostEvent:Connect(function()
          self.RunCtrl:Run({ready:GetEnterPos()})
    end)
    table.insert(self.ConList,con)
end

return M